package com.windfone.web;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Software;
import com.windfone.model.SoftwareType;
import com.windfone.service.SoftwareService;

public class SoftwareTag extends TagSupport {
	/** serialVersionUID */
	private static final long serialVersionUID = 1L;
	private SoftwareService softwareService;
	private List<SoftwareType> softwaretypes;
	private List<Software> softwares;
	private String softwareTypeName;
	private String attriName;	
	private String tagType;
	private int softwareTypeId;
	public SoftwareService getSoftwareService() {
		return softwareService;
	}

	public int getBeginRecord() {
		return beginRecord;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setSoftwareService(SoftwareService softwareService) {
		this.softwareService = softwareService;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}



	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	public String getTagType() {
		return tagType;
	}

	public void setTagType(String tagType) {
		this.tagType = tagType;
	}

	public int getSoftwareTypeId() {
		return softwareTypeId;
	}

	public void setSoftwareTypeId(int softwareTypeId) {
		this.softwareTypeId = softwareTypeId;
	}

	public List<SoftwareType> getSoftwaretypes() {
		return softwaretypes;
	}

	public String getSoftwareTypeName() {
		return softwareTypeName;
	}

	public List<Software> getSoftwares() {
		return softwares;
	}

	public void setSoftwares(List<Software> softwares) {
		this.softwares = softwares;
	}

	public void setSoftwaretypes(List<SoftwareType> softwaretypes) {
		this.softwaretypes = softwaretypes;
	}

	public void setSoftwareTypeName(String softwareTypeName) {
		this.softwareTypeName = softwareTypeName;
	}
	
	public String getAttriName() {
		return attriName;
	}

	public void setAttriName(String attriName) {
		this.attriName = attriName;
	}
 
	 

//	@Override
//	public int doEndTag() throws JspException {
	public int doStartTag() throws JspException {
		ServletContext sc = pageContext.getServletContext();
		if ( softwareService == null )
			softwareService = (SoftwareService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("softwareService");
		 if ( Constant.DOWN_PAGE_SOFTWARE_LIST.equals(tagType) ){
			String whereCondition = "";
			if ( softwareTypeId !=-1 ){
				whereCondition = " AND s.softwareTypeId="+ softwareTypeId;
			}
			PageUtil pu = new PageUtil();
			pu.setCurrentPage(currentPage);
			int totalRecords = softwareService.getAllRecorderNumber(whereCondition) ;
			pu.setTotalRecords( totalRecords );
			pu.setPageSize(Constant.PAGE_SIZE);
			pu.dealPage();
			currentPage = pu.getCurrentPage();
			nextPage = pu.getNextPage();
			totalPage = pu.getTotalPage();
			prevPage = pu.getPrevPage();
			beginRecord = Constant.PAGE_SIZE * (currentPage -1);
			if ( beginRecord< 0 )
				beginRecord = 0; 
			softwares = softwareService.getAllSoftware(beginRecord,whereCondition);	
			this.pageContext.setAttribute(attriName,softwares);
		 }
		 if ( Constant.DOWN_PAGE_TYPE_LIST.equals(tagType) ){
			 softwaretypes = softwareService.getAllSoftwareType();
			 this.pageContext.setAttribute(attriName,softwaretypes);
		 }
//		try {
//			pageContext.getOut().print(	"Hello World This is My Tag=" + commendType);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return super.doStartTag();
		return(SKIP_BODY);
	}
}

